<?php

/**
 * User location
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 13 Jan 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Location extends LB_Controller {

  /**
   * Set location By Id
   * 
   */
  public function set() {

    // Getting location id
    $loc_key = $this->input->get('id', true);

    // Calling location library
    $this->user_location->set($loc_key);

    // Redirect page
    if ($this->is_ajax()) {
      die('true');
    } else {
      $this->redirect($this->redirect_to);
    }
  }

  /**
   * Set Location By Name
   * 
   */
  public function search() {
    if ($this->is_post()) {
      $name = $this->input->post('location_name', true);
      $location = $this->user_location->get_location($name);
      if (!empty($location)) {
        $this->user_location->set($location['loc_key']);
      }
    }
    $this->redirect($this->redirect_to);
  }

  /**
   * Location list for dropdown
   * 
   */
  public function dropdown() {

    // Responding json or Redirect page
    if ($this->is_ajax()) {

      // Getting search query
      $query = $this->input->get('query', true);

      // Preparing countries list
      $country_list = array();
      $get_countries = $this->location_model->get_countries($query);
      if (!empty($get_countries) && is_array($get_countries)) {
        foreach ($get_countries as $country) {
          $country_list[] = array('id' => $country['country_id'] . '@' . 1, 'name' => $country['country_name']);
        }
      }

      // Preparing states list
      $state_list = array();
      $get_states = $this->location_model->get_states($query);
      if (!empty($get_states) && is_array($get_states)) {
        foreach ($get_states as $state) {
          $state_list[] = array('id' => $state['state_id'] . '@' . 2, 'name' => $state['state_name'], 'title' => $state['country_name']);
        }
      }

      // Preparing district list
      $district_list = array();
      $get_districts = $this->location_model->get_districts($query);
      if (!empty($get_districts) && is_array($get_districts)) {
        foreach ($get_districts as $district) {
          $district_list[] = array('id' => $district['district_id'] . '@' . 3, 'name' => $district['district_name'], 'title' => $district['state_name']);
        }
      }

      // Preparing locality list
      $locality_list = array();
      $get_localities = $this->location_model->get_localities($query);
      if (!empty($get_localities) && is_array($get_localities)) {
        foreach ($get_localities as $locality) {
          $locality_list[] = array('id' => $locality['locality_id'] . '@' . 4, 'name' => $locality['locality_name'], 'title' => $locality['district_name']);
        }
      }

      // Merging all three arrays
      $loc_list = array_merge($country_list, $state_list, $district_list, $locality_list);

      // Respond json data
      die(json_encode($loc_list));
    } else {
      $this->redirect($this->redirect_to);
    }
  }

}
